<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>文章列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="__LAYUI__/css/layui.css" rel="stylesheet">
    <style>
        .layui-disabled,
        .layui-disabled:hover {
            color: #cccccc !important;
            cursor: not-allowed !important;
        }
    </style>
</head>

<body>
    <div class="layui-card">
        <div class="layui-card" style="margin-top: 15px;">
            <div class="layui-card-header">文章列表</div>
            <div class="layui-card-body">
                <!-- 搜索工具栏 -->
                <form class="layui-form layui-form-pane" id="searchForm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">文章ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="id" placeholder="文章ID" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="user_id" placeholder="请输入" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" placeholder="请输入" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">文章分类</label>
                            <div class="layui-input-inline">
                                <select name="cate_id">
                                    <option value="">请选择分类</option>
                                    {foreach $cate.data as $k => $v}
                                    <option value="{$v.id}" {$v.pid==0 ? 'disabled' : '' }>{$v.cate_name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn" type="button" lay-submit lay-filter="search">搜索</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>

                <div style="padding: 16px;">
                    <table class="layui-hide" id="test" lay-filter="test"></table>
                </div>

                <script type="text/html" id="toolbarDemo">
                  <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm layui-bg-orange" lay-event="examineAll">一键审核 ( {$art} )</button>
                    <button class="layui-btn layui-btn-sm layui-bg-blue" lay-event="add">添加文章</button>
                    <button class="layui-btn layui-btn-sm layui-bg-red" lay-event="delCheck">删除选中</button>
                    <button class="layui-btn layui-btn-sm layui-btn-primary" id="rowMode">
                      <span>{{= d.lineStyle ? '多行' : '单行' }}模式</span>
                      <i class="layui-icon layui-icon-down layui-font-12"></i>
                    </button>
                  </div>
                </script>

                <script type="text/html" id="toolDemo">
                    <div class="layui-clear-space">
                        <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-xs layui-bg-gray" lay-event="more">
                            更多 
                            <i class="layui-icon layui-icon-down"></i>
                        </a>
                    </div>
                </script>

            </div>
        </div>
    </div>



    <script src="__LAYUI__/layui.js"></script>
    <script>
        layui.use(['table', 'dropdown'], function () {
            var table = layui.table;
            var dropdown = layui.dropdown;
            var form = layui.form;
            layer = layui.layer;
            $ = layui.$;

            // 表单搜索
            form.on('submit(search)', (res) => {
                field = res.field;
                table.reload('test', { where: field });
                return false; // 或添加这行
            });

            // 创建渲染实例
            table.render({
                elem: '#test',
                url: '/admin/article/list',
                toolbar: '#toolbarDemo',
                parseData: function (res) {
                    return {
                        "code": 0,
                        "msg": res.message,
                        "count": res.data.total,
                        "data": res.data.data
                    };
                },
                cellMinWidth: 80,
                totalRow: true,
                page: true,
                cols: [[
                    { type: 'checkbox' },
                    { field: 'id', width: 100, title: '文章ID' },
                    { field: 'user_id', width: 100, title: '用户UID' },
                    { field: 'title', width: 200, title: '标题' },
                    { field: 'content', width: 200, title: '内容' },
                    {
                        field: 'hide_content',
                        width: 200,
                        title: '内容',
                        templet: function (d) {
                            if (d.type != 1) {
                                return '[#]'
                            } else {
                                return d.hide_content
                            }
                        }
                    },
                    {
                        field: 'category',
                        width: 200,
                        title: '文章分类',
                        templet: function (d) {
                            if (d.category == null) {
                                return '分类已删除'
                            } else {
                                return d.category.cate_name
                            }
                        }
                    },
                    {
                        field: 'status',
                        width: 100,
                        title: '文章状态',
                        templet: function (d) {
                            if (d.status == 0) {
                                return `<span class="layui-font layui-font-orange">待审核</span>`
                            } else if (d.status == 2) {
                                return `<span class="layui-font layui-font-red">锁定</span>`
                            } else {
                                return `<span class="layui-font layui-font-blue">正常</span>`
                            }
                        }
                    },
                    {
                        field: 'type',
                        width: 100,
                        title: '文章类型',
                        templet: function (d) {
                            if (d.type == 1) {
                                return `<span class="layui-font layui-font-black">默认</span>`
                            } else if (d.status == 2) {
                                return `<span class="layui-font layui-font-black">积分</span>`
                            } else {
                                return `<span class="layui-font layui-font-black">回复</span>`
                            }
                        }
                    },
                    {
                        field: 'is_hot',
                        width: 100,
                        title: '是否加精',
                        templet: function (d) {
                            if (d.is_hot == 1) {
                                return `<span class="layui-font layui-font-black">是</span>`
                            } else {
                                return `<span class="layui-font layui-font-gray">否</span>`
                            }
                        }
                    },
                    {
                        field: 'is_top',
                        width: 100,
                        title: '是否置顶',
                        templet: function (d) {
                            if (d.is_top == 1) {
                                return `<span class="layui-font layui-font-black">是</span>`
                            } else {
                                return `<span class="layui-font layui-font-gray">否</span>`
                            }
                        }
                    },
                    { field: 'score', width: 100, title: '积分', sort: true },
                    { field: 'create_time', title: '发布时间', width: 120 },
                    { fixed: 'right', title: '操作', width: 134, minWidth: 125, templet: '#toolDemo' }
                ]],
                done: function () {
                    // 行模式下拉菜单
                    dropdown.render({
                        elem: '#rowMode',
                        data: [{
                            id: 'default-row',
                            title: '单行模式（默认）'
                        }, {
                            id: 'multi-row',
                            title: '多行模式'
                        }],
                        click: function (obj) {
                            switch (obj.id) {
                                case 'default-row':
                                    table.reload('test', {
                                        lineStyle: null
                                    });
                                    layer.msg('已设为单行');
                                    break;
                                case 'multi-row':
                                    table.reload('test', {
                                        lineStyle: 'height: 95px;'
                                    });
                                    layer.msg('已设为多行');
                                    break;
                            }
                        }
                    });
                }
            });

            // 工具栏事件
            table.on('toolbar(test)', function (obj) {
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch (obj.event) {
                    case 'delCheck':
                        var data = checkStatus.data;
                        if (data.length === 0) {
                            return layer.msg('请先选择要删除的文章');
                        }
                        var ids = data.map(item => item.id).join(',');
                        layer.confirm('确定删除选中的文章吗?', function (index) {
                            $.get('/admin/article/del', { id: ids }, (res) => {
                                if (res.code == 200) {
                                    table.reload('test');
                                    layer.msg('删除成功');
                                } else {
                                    layer.msg('删除失败');
                                }
                            });
                        });
                        break;

                    case 'examineAll':
                        var selectedData = checkStatus.data;
                        layer.open({
                            title: '审核文章',
                            content: '请选择审核方式',
                            btn: ['审核选中', '审核全部'],
                            yes: function (index) {
                                // 审核选中的逻辑
                                if (selectedData.length === 0) {
                                    layer.msg('请先选择要审核的文章');
                                    return;
                                }
                                var ids = selectedData.map(item => item.id).join(',');
                                $.get('/admin/article/examineAll', { id: ids, type: 2 }, function (res) {
                                    if (res.code == 200) {
                                        layer.msg('选中文章审核成功');
                                        table.reload('test');
                                    } else {
                                        layer.msg('操作失败');
                                    }
                                    layer.close(index);
                                });
                            },
                            btn2: function (index) {
                                // 审核全部的逻辑
                                $.get('/admin/article/examineAll', function (res) {
                                    if (res.code == 200) {
                                        layer.msg('全部文章审核成功');
                                        table.reload('test');
                                    } else {
                                        layer.msg('操作失败');
                                    }
                                    layer.close(index);
                                });
                            }
                        });
                        break;

                    case 'add':
                        layer.open({
                            title: '添加文章',
                            type: 2,
                            area: ['80%', '80%'],
                            content: '/admin/article/add'
                        });
                        break;
                }
            });

            // 行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    layer.open({
                        title: '编辑 - id:' + data.id,
                        type: 2,
                        area: ['80%', '80%'],
                        content: '/admin/article/edit/?id=' + data.id
                    });
                } else if (obj.event === 'more') {
                    dropdown.render({
                        elem: this,
                        show: true,
                        data: [{
                            title: '查看',
                            id: 'detail'
                        }, {
                            title: '删除',
                            id: 'del'
                        }],
                        click: function (menudata) {
                            if (menudata.id === 'detail') {
                                layer.open({
                                    title: '查看文章',
                                    type: 2,
                                    area: ['80%', '80%'],
                                    content: '/admin/article/read?id=' + data.id
                                });
                            } else if (menudata.id === 'del') {
                                layer.confirm('确定删除文章 [' + data.title + '] 吗?', function (index) {
                                    $.get('/admin/article/del', { id: data.id }, (res) => {
                                        if (res.code == 200) {
                                            obj.del(); // 删除对应行（tr）的DOM结构
                                            layer.close(index);
                                            layer.msg('删除成功')
                                        } else {
                                            layer.msg('删除失败')
                                        }
                                    })

                                    // 向服务端发送删除指令
                                });
                            }
                        },
                        id: 'dropdown-table-tool',
                        align: 'right', // 右对齐弹出
                        style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
                    });
                }
            });
            // table 滚动时移除内部弹出的元素
            var tableInst = table.getOptions('test');
            tableInst.elem.next().find('.layui-table-main').on('scroll', function () {
                dropdown.close('dropdown-table-tool');
            });

            // 触发表格复选框选择
            table.on('checkbox(test)', function (obj) {
                console.log(obj)
            });

            // 触发表格单选框选择
            table.on('radio(test)', function (obj) {
                console.log(obj)
            });

            // 行单击事件
            table.on('row(test)', function (obj) {
                //console.log(obj);
                //layer.closeAll('tips');
            });
            // 行双击事件
            table.on('rowDouble(test)', function (obj) {
                console.log(obj);
            });

            // 单元格编辑事件
            table.on('edit(test)', function (obj) {
                var field = obj.field; // 得到字段
                var value = obj.value; // 得到修改后的值
                var data = obj.data; // 得到所在行所有键值
                // 值的校验
                if (field === 'email') {
                    if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(obj.value)) {
                        layer.tips('输入的邮箱格式不正确，请重新编辑', this, { tips: 1 });
                        return obj.reedit(); // 重新编辑 -- v2.8.0 新增
                    }
                }
                // 编辑后续操作，如提交更新请求，以完成真实的数据更新
                // …
                layer.msg('编辑成功', { icon: 1 });

                // 其他更新操作
                var update = {};
                update[field] = value;
                obj.update(update);
            });
        });
    </script>
</body>

</html>